home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / ProcessBar™ / Code / Sources / sample win.c < prev    next >
Encoding:
Text File  |  1995-09-10  |  5.4 KB  |  197 lines  |  [TEXT/MMCC]

  1. // File "sample win.c" -
  2.  
  3. #include "main.h"
  4. #include "floaters.h"
  5. #include "prefs.h"
  6. #include "sample drag.h"
  7. #include "sample win.h"
  8.  
  9. #include "mdg.h"
  10.  
  11. // ***********************************************************************************
  12. // Global Declarations 
  13.  
  14. extern GlobalsRec glob;
  15.  
  16.  
  17. // ***********************************************************************************
  18. // ***********************************************************************************
  19.  
  20. WindowPtr NewSampleWindow() {
  21.     static short count = 0, **prefCount;
  22.     Rect bounds; 
  23.     WindowPtr win;
  24.     Str255 textBuff = "\pSample Window ", altBuff;
  25.     StringPtr winTextPtr;
  26.  
  27.     // Demo the prefs functions by incrementing the Window #
  28.     if (prefCount = (short **) GetPrefs('WCnt', 1)) {
  29.         count = ++(**prefCount);
  30.         WritePrefs((Handle) prefCount, 'WCnt', 1);
  31.         DisposeHandle((Handle) prefCount);
  32.         }
  33.       else if (prefCount = (short **) NewHandle(sizeof(**prefCount))) {
  34.         **prefCount = ++count;
  35.         WritePrefs((Handle) prefCount, 'WCnt', 1);
  36.         DisposeHandle((Handle) prefCount);
  37.         }
  38.       else count++;
  39.  
  40.     // Setup the Window Title String
  41.     NumToString(count, altBuff);
  42.     BlockMove(altBuff+1, textBuff + textBuff[0] + 1, altBuff[0]);
  43.     textBuff[0] += altBuff[0];
  44.  
  45.     SetRect(&bounds, qd.screenBits.bounds.left, qd.screenBits.bounds.bottom-kTaskbarHeight/*kTaskbarScreenBottomMargin*/, qd.screenBits.bounds.right, qd.screenBits.bounds.bottom+1/*+kTaskbarHeight-kTaskbarScreenBottomMargin*/);
  46. //    OffsetRect(&bounds, (count-1) % 3 * 40, (count-1) % 3 * 40);
  47.     win = NewFloater(0, &bounds, textBuff, TRUE, /*2048*/ 2, (WindowPtr) -1, TRUE, 0, 0,
  48.             SampleWindowEventHandler, DisposeSampleWindow);
  49.  
  50.     // Apply some text to draw for update routines
  51.     if (win)
  52.     {
  53.         {
  54.             GrafPtr savedPort;
  55.             
  56.             // remember this window (forever)
  57.             gTaskbarWindow = win;
  58.             
  59.             // quickly draw the gray background
  60.             GetPort(&savedPort);
  61.             SetPort(win);
  62.         
  63.             RGBBackColor(&kBgGrayColor);
  64.             EraseRect(&win->portRect);
  65.             DrawMacOSIcon();
  66.             SetPort(savedPort);
  67.             
  68.             // build the list of processes
  69.             UpdateProcessArray();
  70.             
  71.             gTaskbarVisible = TRUE;
  72.             
  73.             // make my process invisible
  74.             
  75.         }
  76.         
  77. /*        SetSampleWindowText(win,
  78. "\pAppeWin 2.0: A free demo program and source code by Matt Slot (fprefect@umich.edu) \
  79. that shows off TSM Floaters.\r\rThe latest version is free to look at or use, and can \
  80. be downloaded from your local Sumex or Mac-Archives mirror site.");
  81. */
  82.     }
  83.     
  84.     if (win && glob.hasDragMgr) SetupDragHandlers(win);
  85.         
  86.     return(win);
  87.     }
  88.  
  89. // ***********************************************************************************
  90. // ***********************************************************************************
  91.  
  92. void DisposeSampleWindow(WindowPtr win) {
  93.     StringPtr winTextPtr;
  94.     
  95.     if (winTextPtr = (StringPtr) GetWRefCon(win)) DisposePtr((Ptr) winTextPtr);
  96.     DisposeFloater(win);
  97.     
  98.     // For a background only app, this will cause us to quit when the user
  99.     //   closes the last floater. Its your choice if you want this to happen.
  100.     if (glob.bkgdOnly && ! GetIndFloater(1, FALSE)) glob.quitting = TRUE;
  101.     }
  102.  
  103. // ***********************************************************************************
  104. // ***********************************************************************************
  105.  
  106. void SampleWindowEventHandler(EventRecord *theEvent, WindowPtr win) {
  107.     Rect destRect;
  108.     GrafPtr savePort;
  109.     StringPtr winTextPtr;
  110.  
  111.     switch(theEvent->what) {
  112.         case nullEvent:
  113.             break;
  114.         case mouseDown: {
  115.             // The Window part has already been found and forwarded to us
  116.             short thePart = theEvent->message;    
  117.             
  118.             switch(thePart) {
  119.                 case inContent:
  120.                     DoClickInTaskbar(theEvent, win);
  121.                     break;
  122.                 case inGrow:
  123.                     break;
  124.                 case inGoAway:
  125.                     if (TrackGoAway(win, theEvent->where))
  126.                         DisposeSampleWindow(win);
  127.                     break;
  128.                 case inZoomIn:
  129.                     break;
  130.                 case inZoomOut:
  131.                     break;
  132.                 }
  133.             break;
  134.             }
  135.         case keyDown:
  136.         case autoKey:
  137.             break;
  138.         case updateEvt: {
  139.             GetPort(&savePort);
  140.             SetPort(win);
  141.             BeginUpdate(win);
  142.             
  143.             UpdateProcessArray();
  144.             DrawTaskbar(win);
  145.             
  146. /*
  147.             // Draw something cool!
  148.             destRect = win->portRect;
  149.             EraseRect(&destRect);
  150.             InsetRect(&destRect, 4, 4);
  151.             TextFont(1); TextSize(9);
  152.             if (winTextPtr = GetSampleWindowText(win)) 
  153.                 TextBox(winTextPtr+1, winTextPtr[0], &destRect, teFlushDefault);
  154.             TextFont(0); TextSize(0);
  155. */            
  156.             EndUpdate(win);
  157.             SetPort(savePort);
  158.             break;
  159.             }
  160.         }
  161.     }
  162.  
  163. // ***********************************************************************************
  164. // ***********************************************************************************
  165.  
  166. void SetSampleWindowText(WindowPtr win, StringPtr winText) {
  167.     Boolean dirty = FALSE;
  168.     Ptr storage = (Ptr) GetWRefCon(win);
  169.     GrafPtr savePort;
  170.     
  171.     if (! storage) { storage = NewPtr(sizeof(Str255)); dirty = TRUE; }
  172.     if (! storage) return;
  173.     
  174.     if (winText && ! EqualString(winText, (StringPtr) storage, 0, 0)) dirty = TRUE;
  175.     BlockMove((winText && winText[0]) ? winText : "\p", storage, sizeof(Str255));
  176.     SetWRefCon(win, (long) storage);
  177.     
  178.     // Force a redraw after changing content
  179.     if (dirty) {
  180.         GetPort(&savePort);
  181.         SetPort(win);
  182.         InvalRect(&win->portRect);
  183.         SetPort(savePort);
  184.         }
  185.     }
  186.  
  187. // ***********************************************************************************
  188. // ***********************************************************************************
  189.  
  190. StringPtr GetSampleWindowText(WindowPtr win) {
  191.     Ptr storage = (Ptr) GetWRefCon(win);
  192.     StringPtr winText;
  193.  
  194.     winText = (storage && ((StringPtr) storage[0])) ? ((StringPtr) storage) : "\p<No Data>";
  195.     return(winText); 
  196.     }
  197.